package xcmg.device.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author ZHANGK
 * @date 2020/6/22
 **/
@Controller
@RequestMapping("/image")
public class ImageController {


    @Value("${spring.file.download.path}")
    private String filePath;

    @RequestMapping(value = "/read/**", method = {org.springframework.web.bind.annotation.RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<?> showImage(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        String url = request.getRequestURI();
        String imgName = url.substring(url.indexOf("/image/read/")+12);
        InputStream inputStream = new FileInputStream(new File(filePath+"image/"+imgName));
        InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
        response.setContentType("image/png;charset=UTF-8");
        return ResponseEntity.ok(inputStreamResource);
    }

}
